package com.bulletelc.apollopre.module.display.product.resultList.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.SearchResultGoodsBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;

import java.util.List;

/**
 * author: lhl
 * date  : 2017/12/26
 * desc  : 产品列表 adapter
 */


public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {

    private LayoutInflater inflater;
    private List<SearchResultGoodsBean> searchResults;
    private Context context;
    private OnItemClickListener onItemClickListener;

    // 布局切换，true为grid，false为linear;
    private boolean type = true;

    public ProductListAdapter( Context context,List<SearchResultGoodsBean> searchResults) {
        this.context = context;
        this.searchResults = searchResults;
    }

    public ProductListAdapter( Context context,List<SearchResultGoodsBean> searchResults, Boolean type) {
        this.context = context;
        this.searchResults = searchResults;
        this.type = type;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ProductListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        ViewHolder viewHolder;
        if (type) {
            viewHolder = new ViewHolder(inflater.inflate(R.layout.search_result_list_item1, parent, false));
        } else {
            viewHolder = new ViewHolder(inflater.inflate(R.layout.search_result_list_item2, parent, false));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ProductListAdapter.ViewHolder holder, final int position) {
        SearchResultGoodsBean result = searchResults.get(position);
        holder.tvName.setText(result.getGoodName());
        holder.tvSalePrice.setText("¥" + result.getLowest_price());
        holder.tvSold.setText("已售:" + result.getSold());
        holder.tvComments.setText(result.getComment_count() + "人已评价");
        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.ivPic, result.getImgUrl()));
        if(onItemClickListener != null) {
            holder.cvItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return searchResults.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvName, tvSalePrice, tvComments, tvSold;
        ImageView ivPic;
        LinearLayout cvItem;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.ivPic);
            tvName = itemView.findViewById(R.id.tvName);
            tvSalePrice = itemView.findViewById(R.id.tvSalePrice);
            tvSold = itemView.findViewById(R.id.tvSold);
            tvComments = itemView.findViewById(R.id.tvComments);
            cvItem = itemView.findViewById(R.id.cv_item);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}
